home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / EleOfC++ vrs 0.1 / Scribble / TScribbleDoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-14  |  4.5 KB  |  251 lines  |  [TEXT/KAHL]

  1. /*
  2. ** This is the TScribbleDoc class definition.  This class is an adaptation of 
  3. ** the class of the same name in the book, Elements of C++ Macintosh 
  4. ** Programming, by Dan Weston.
  5. **
  6. ** Copyright © 1991 Mark Gross 
  7. ** (RR2, Box 84, Clayville, NY 13322);         
  8. ** this file may be published everywhere, but no charge
  9. ** may be made for its use.  Please contact me about any bugs found.
  10. ** I'm also looking for Macintosh development work, so drop me a line
  11. ** if you think I could help.  
  12. */
  13.  
  14. #include "TScribbleDoc.h"
  15. #include "magics.h"
  16.  
  17. TDoc*    TScribbleDoc::Init(OSType theCreator, SFReply *reply)
  18. {
  19.     inherited::Init(theCreator, reply);
  20.     
  21.     fPattern = patGray;
  22.     fPenSize = 2;
  23.     fPic = NULL;
  24.     return this;
  25.     
  26. }/*end of function*/
  27.  
  28. void    TScribbleDoc::SetPenSize(short p)
  29. {
  30.     fPenSize = p;
  31. }
  32.  
  33. void    TScribbleDoc::SetPenPat(penPat p)
  34. {
  35.     fPattern = p;
  36. }
  37.  
  38. short    TScribbleDoc::GetPenSize(void)
  39. {
  40.     return    fPenSize;
  41. }
  42.  
  43. penPat    TScribbleDoc::GetPenPat(void)
  44. {
  45.     return fPattern;
  46. }
  47.  
  48. void    TScribbleDoc::DoContent(EventRecord* theEvent)
  49. {
  50.     register Point    newPoint;
  51.     
  52.     if(fDocWindow)
  53.     {
  54.         SetPort(fDocWindow);
  55.         PenSize(fPenSize, fPenSize);
  56.         
  57.         if(fPattern == patBlack)
  58.             PenPat(black);
  59.         if(fPattern == patGray)
  60.             PenPat(gray);
  61.         if(fPattern == patWhite)
  62.             PenPat(white);
  63.         
  64.         GlobalToLocal( &theEvent->where);
  65.         MoveTo(theEvent->where.h, theEvent->where.v);
  66.         do
  67.         {
  68.             GetMouse(&newPoint);
  69.             LineTo(newPoint.h, newPoint.v);
  70.         } while(StillDown());
  71.         
  72.         fNeedtoSave = TRUE;
  73.         
  74.         fPic = OpenPicture(&fDocWindow->portRect);
  75.         CopyBits(&fDocWindow->portBits, &fDocWindow->portBits,
  76.                 &fDocWindow->portRect, &fDocWindow->portRect, srcCopy, NULL);
  77.         ClosePicture();
  78.     }/*end of window*/
  79. }/*end of function*/
  80.  
  81.  
  82. void    TScribbleDoc::Draw(Rect *r)
  83. {
  84.  
  85.     if(fPic != NULL)
  86.         DrawPicture(fPic, &( (*fPic)->picFrame ) );
  87. }
  88.  
  89. OSType    TScribbleDoc::GetDocType(void)
  90. {
  91.     return 'SPCT';
  92. }
  93.  
  94.  
  95. Boolean    TScribbleDoc::WriteDocFile(short refNum)
  96. {
  97.     long    len;
  98.     OSErr    err;
  99.     
  100.     if(fDocWindow)
  101.     {
  102.         if(fPic != NULL)
  103.         {
  104.             len = GetHandleSize(fPic );
  105.             HLock(fPic);
  106.             err = SetFPos(refNum, fsFromStart, 0L);
  107.             err = FSWrite( refNum, &len, *fPic);
  108.             HUnlock(fPic);
  109.             
  110.             if(err == noErr)
  111.                 return TRUE;
  112.             else
  113.                 return FALSE;
  114.         }
  115.     }
  116. }/*end of function*/
  117.  
  118.  
  119. Boolean    TScribbleDoc::ReadDocFile(short refNum)
  120. {
  121.     long    len;
  122.     short    height, width;
  123.     OSErr    err;
  124.     Rect    r;
  125.     PicHandle    thePic;
  126.     
  127.     if(fDocWindow)
  128.     {
  129.         err = GetEOF(refNum, &len);
  130.         thePic = (PicHandle)NewHandle(len);
  131.         if(thePic == NULL)
  132.         {
  133.             ErrorAlert(rDocErrorStrings, sNoMem);
  134.             return FALSE;
  135.         }
  136.         
  137.         HLock(thePic);
  138.         err = SetFPos(refNum, fsFromStart, 0L);
  139.         FSRead(refNum, &len, *thePic);
  140.         if(err == noErr)
  141.         {
  142.             r = (*thePic)->picFrame;
  143.             height = r.bottom - r.top;
  144.             width = r.right - r.left;
  145.             r = screenBits.bounds;
  146.             height = min(height, r.bottom - r.top - kHAdjust);
  147.             width = min(width, r.right - r.left - kWAdjust);
  148.             SizeWindow(fDocWindow, width, height, TRUE);
  149.             
  150.             fPic = thePic;
  151.             return TRUE;
  152.         }
  153.         else
  154.         {
  155.             DisposHandle(thePic);
  156.             return FALSE;
  157.         }
  158.     }/* end if window*/
  159.     return FALSE;
  160. }/*end of function*/
  161.  
  162.     
  163. Boolean    TScribbleDoc::DoDocMenuCommand(short menuID, short menuItem)
  164. {
  165.     if(menuID == rPenMenu)
  166.     {
  167.         switch(menuItem)
  168.         {
  169.             case i1X1:
  170.                 SetPenSize(1);
  171.                 break;
  172.             case i2X2:
  173.                 SetPenSize(2);
  174.                 break;
  175.             case i3X3:
  176.                 SetPenSize(3);
  177.                 break;
  178.             case iBlack:
  179.                 SetPenPat(patBlack);
  180.                 break;
  181.             case iGray:
  182.                 SetPenPat(patGray);
  183.                 break;
  184.             case iWhite:
  185.                 SetPenPat(patWhite);
  186.                 break;
  187.             default:
  188.                 return FALSE;
  189.         }/*end switch menuitem*/
  190.         return TRUE;
  191.     }
  192.     else
  193.     {
  194.         return inherited::DoDocMenuCommand(menuID, menuItem);
  195.     }
  196. }/*end of function*/
  197.             
  198. Boolean    TScribbleDoc::CanSaveAs(void)
  199. {
  200.     return TRUE;
  201. }
  202.  
  203. void    TScribbleDoc::AdjustDocMenus(void)
  204. {
  205.     MenuHandle    menu;
  206.     
  207.     menu = GetMHandle(rPenMenu);
  208.     CheckItem(menu,i1X1, GetPenSize() == 1);
  209.     CheckItem(menu,i2X2, GetPenSize() == 2);
  210.     CheckItem(menu,i3X3, GetPenSize() == 3);
  211.     CheckItem(menu, iBlack, GetPenPat() == patBlack);
  212.     CheckItem(menu, iGray, GetPenPat() == patGray);
  213.     CheckItem(menu, iWhite, GetPenPat() == patWhite);
  214.     
  215.     inherited::AdjustDocMenus();
  216. }/*end of function*/
  217.     
  218. void    TScribbleDoc::TogglePenMenu(Boolean enable)
  219. {
  220.     MenuHandle    menu;
  221.     
  222.     menu = GetMHandle(rPenMenu);
  223.     SetMenuAbility(menu, kEveryItem, enable);
  224.     DrawMenuBar();
  225. }/*end of function*/
  226.  
  227. void    TScribbleDoc::Activate(void)
  228. {
  229.     inherited::Activate();
  230.     TogglePenMenu(TRUE);
  231. }
  232.  
  233. void    TScribbleDoc::Deactivate(void)
  234. {
  235.     inherited::Deactivate();
  236.     TogglePenMenu(FALSE);
  237. }
  238.  
  239. Boolean    TScribbleDoc::DoClose(void)
  240. {
  241.     if(inherited::DoClose())
  242.     {
  243.         TogglePenMenu(FALSE);
  244.         return TRUE;
  245.     }
  246.     else
  247.         return FALSE;
  248. }/*end of function*/
  249.  
  250.  
  251.